<?php
/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file secturity.func.php
 * @author SkyWorld
 * @date 2011-1-17
 * @description This file contains all funtions about secturity
 **/

if(!defined('IN_NCG')) exit ('Access Denied.');

    /**
	 * @name encode
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description This function uses the MCrypt library to encrypt some data
	 * 				The data will then be stored in an array as will the encryption IV
     * @parameters  $data: the data to be encode
     * 				$key : the private key when the encode process needs,
     * 					   this key should be stored in a safe place
     * @return array : This array contains the encoded data and encryption IV
     **/

function encode($data, $key)
{
	//Create the key
	$key = md5($key);

	//Open the cipher, using Rijndael 256 in CBC mode
	$m = mcrypt_module_open('rijndael-256', '', 'cbc', '');

	/**
	 *  Create the IV,if in linux, do it like below
	 *  $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m),MCRYPT_DEV_RAND);
	 **/
	srand();
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m),MCRYPT_RAND);

	//Initialize the encryption
	mcrypt_generic_init($m, $key, $iv);

	//Encrypt the data
	$data = mcrypt_generic($m, $data);

	//cleaning job for security reason, close the encryption hander and the cipher
	mcrypt_generic_deinit($m);
	mcrypt_module_close($m);

	$arr = array( 'data' => base64_encode($data),'iv'   => base64_encode($iv));

	return $arr;
}


    /**
	 * @name decode
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description This function uses the MCrypt library to decrypt some data
     * @parameters  $data: the data to be decode
     * 				$iv	 : the encryption IV that created before
     * 				$key : the private key when the encode process needs,
     * 					   this key should be stored in a safe place
     * @return stirng : the decoded data
     **/
function decode($data, $iv, $key)
{
	//create the key
	$key = md5($key);

	//Open the cipher, using Rijndael 256 in CBC mode
	$m = mcrypt_module_open('rijndael-256', '', 'cbc', '');

	//Decode the IV
	$iv = base64_decode($iv);

	//Initialize the encryption
	mcrypt_generic_init($m, $key, $iv);

	//Decrypt the data:
	$data = mdecrypt_generic($m,base64_decode($data));

	//cleaning job for security reason, close the encryption hander and the cipher
	mcrypt_generic_deinit($m);
	mcrypt_module_close($m);

	return trim($data);
}

	/**
 	 * @name formhash
 	 * @author deltamaster
	 * @date 2011-1-17
 	 * @description Hash a form to prevent flood.
 	 * @parameters void
 	 * @return
 	 * 	string: The form hash.
 	 * @date 2010-01-28
	 */
function formhash()
{
	global $timestamp, $uid, $username;
	return substr(md5(substr($timestamp, 0, -7)).$uid.$username, 8, 8);
}

/**
 * @name submitcheck
 * @author deltamaster
 * @level KERNEL
 * @description Check whether a specific submit button is clicked, or whether some information has been sent. After this function, you can use these information as local variables.
 * @parameters
 * 	string $var: The name of a submit button.
 *  bool $allowget: Whether allow "get" method to send data. False by default.
 * @return
 * 	bool: Return true if the specific submit button is clicked, else return false.
 * @date 2010-01-27
 */
function submitcheck($var, $allowget = false)
{
	if (empty($_POST[$var]) && ($allowget ? empty($_GET[$var]) : true))
		return false;
	else
	{
		if (!$allowget && (isset($_POST['formhash']) ? $_POST['formhash'] != formhash() : true))
			die("invalid request");
		foreach ($_POST as $key => $value)
			$GLOBALS[$key] = $value;
		if ($allowget)
		{
			foreach ($_GET as $key => $value)
				$GLOBALS[$key] = $value;
		}
		return true;
	}
	//not finished, but can be used.
}





?>
